package com.admission.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class EnrollUtils {
	private static POIFSFileSystem fs;
	private static HSSFWorkbook wb;
	private static HSSFSheet sheet;
	private static HSSFRow row;
	private static BufferedImage image;
	private static int wordWidth = 17;// 字的宽度
	private static int wordHeigh = 17;// 字的高度
	private static int[] cellWidths;
	private static int[] cellHeighs;

	public static void genPicFromExcel(String excelPath, String picPath)
			throws IOException {
		int imageWidth = 0;// 图片的宽度
		int imageHeight = 0;// 图片的高度
		InputStream is = new FileInputStream(excelPath);
		fs = new POIFSFileSystem(is);
		wb = new HSSFWorkbook(fs);
		sheet = wb.getSheetAt(0);
		// 得到总行数
		int rowNum = sheet.getPhysicalNumberOfRows();
		row = sheet.getRow(0);
		int colNum = row.getPhysicalNumberOfCells();
		cellWidths = new int[colNum];
		cellHeighs = new int[rowNum];
		// 计算各行，各列高度和宽度，并计算总的
		for (int i = 0; i < rowNum; i++) {
			cellHeighs[i] = sheet.getRow(i).getHeight() / 15;
			imageHeight += cellHeighs[i];
		}
		for (int i = 0; i < colNum; i++) {
			cellWidths[i] = sheet.getColumnWidth(i) / 30;
			imageWidth += cellWidths[i];
		}
		image = new BufferedImage(imageWidth, imageHeight,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D) image.getGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, imageWidth, imageHeight);
		g.setColor(Color.BLACK);
		g.setFont(new Font("宋体", Font.BOLD, 15));
		for (int i = 0, sum = 0; i < cellWidths.length; i++) {
			g.drawLine(sum, 0, sum, imageHeight);
			sum += cellWidths[i];
		}
		for (int i = 0, sum = 0; i < cellHeighs.length; i++) {
			g.drawLine(0, sum, imageWidth, sum);
			sum += cellHeighs[i];
		}

		g.drawString("专业名称", cellWidths[0] / 2 - 2 * wordWidth, cellHeighs[0]
				/ 2 + wordHeigh / 2);

		// 打印第二行一下的内容
		for (int r = 1; r < rowNum; r++) {
			for (int c = 0; c < colNum; c++) {
				if (c == 0 && r == 1) {
					g.drawString("合计", cellWidths[0] / 2 - wordWidth,
							cellHeighs[0] + cellHeighs[1] / 2 + wordHeigh / 2);
				} else {
					// 对位置进行了微调
					g.drawString(
							getCellFormatValue(sheet.getRow(r).getCell(c)),
							getSum(c - 1, cellWidths) + 6,
							getSum(r, cellHeighs) - 4);
				}
			}
		}
		for (int i = 1; i < colNum; i++) {
			String s = getCellFormatValue(sheet.getRow(0).getCell(i));
			for (int j = 0; j < s.length(); j++) {
				String t = s.substring(j, j + 1);
				// 位置有微调
				g.drawString(t, getSum(i - 1, cellWidths) + 6, (j + 1)
						* wordHeigh);
			}
		}
		ImageIcon imageIcon = new ImageIcon(picPath);
		g.drawImage(imageIcon.getImage(), 230, 0, null);
		createImage(picPath);
		// System.out.println(123);
	}

	private static void createImage(String fileLocation) {
		try {
			FileOutputStream fos = new FileOutputStream(fileLocation);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
			encoder.encode(image);
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 格式化表格内的数据
	public static String getCellFormatValue(Cell cell) {
		String cellvalue = "";
		if (cell != null) {
			// 判断当前Cell的Type
			switch (cell.getCellType()) {
			// 如果当前Cell的Type为NUMERIC
			case HSSFCell.CELL_TYPE_NUMERIC:
			case HSSFCell.CELL_TYPE_FORMULA: {
				// 判断当前的cell是否为Date
				if (HSSFDateUtil.isCellDateFormatted(cell)) {
					// 如果是Date类型则，转化为Data格式
					// 方法1：这样子的data格式是带时分秒的：2011-10-12 0:00:00
					// cellvalue = cell.getDateCellValue().toLocaleString();
					// 方法2：这样子的data格式是不带带时分秒的：2011-10-12
					Date date = cell.getDateCellValue();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					cellvalue = sdf.format(date);
				}
				// 如果是纯数字
				else {
					// 取得当前Cell的数值
					cellvalue = String
							.valueOf((int) cell.getNumericCellValue());
				}
				break;
			}
			// 如果当前Cell的Type为STRIN
			case HSSFCell.CELL_TYPE_STRING:
				// 取得当前的Cell字符串
				cellvalue = cell.getRichStringCellValue().getString();
				break; // 默认的Cell值
			default:
				cellvalue = " ";
			}
		} else {
			cellvalue = "";
		}
		return cellvalue;
	}

	/**
	 * 获取数组前end个数的和
	 * 
	 * @param end
	 * @param array
	 * @return
	 */
	public static int getSum(int end, int[] array) {
		int result = 0;
		for (int i = 0; i <= end; i++) {
			result += array[i];
		}
		return result;
	}


}
